﻿using System.Text;
using KinonekoSoftware.Platform.Windows;
using KinonekoSoftware.JuXiaoYouDB.Core;

namespace KinonekoSoftware.Studio.Components
{
    public sealed class RepositoryManagerViewModel : ObjectDialog<string>
    {
        private string _directory;

        public RepositoryManagerViewModel()
        {
            SelectedCommand = CreateCommand(DoSelectedCommand);
            ClearCommand    = CreateCommand(DoClearCommand);
        }
        
        //-------------------------------------------------------------
        //
        //          Static Methods
        //
        //-------------------------------------------------------------

        public static string GetRepositorySetting()
        {
            var ai              = Ioc.Get<ApplicationInformation>();
            var settingFileName = Path.Combine(ai.SettingFolder, "JuXiaoYou5-repo.cfg");
            return settingFileName;
        }

        public static string SetRepositorySetting(string fileName)
        {
            File.WriteAllText(GetRepositorySetting(), fileName, Encoding.UTF8);
            return fileName;
        }
        
        public static bool HasRepositorySetting()
        {
            return File.Exists(GetRepositorySetting());
        }

        public static Task<Result<string>> Select()
        {
            return Dialog<string, RepositoryManagerViewModel>();
        }

        public static async Task EnsureRepositoryAsync(Action callback)
        {
            var repoMgr = Ioc.Get<IRepositoryManager>();
            
            if (!HasRepositorySetting() || !repoMgr.IsExists)
            {
                var r = await Select();

                if (!r.IsFinished)
                {
                    return;
                }
                
                
                //
                //
                SetRepositorySetting(r.Value);
                repoMgr.Directory = r.Value;
               
                //
                //
                await Success(TitleSR.SuccessOperation, TextSR.Successful_Save);
            }
            
            //
            //
            callback?.Invoke();
        }
        
        //-------------------------------------------------------------
        //
        //          Override Methods
        //
        //-------------------------------------------------------------
        protected override void CreateValidationRules(ICollection<DialogRule> rules)
        {
            rules.Add(DialogRule.Create<RepositoryManagerViewModel>(TextSR.Parameter_Empty_Value, x => !string.IsNullOrEmpty(_directory)));
        }

        protected override string OnFinish(bool editing)
        {
            
            //
            //
            return _directory;
        }

        //-------------------------------------------------------------
        //
        //          Command Methods
        //
        //-------------------------------------------------------------
        private void DoSelectedCommand()
        {
#if WINDOWS
            var opendlg = Shell.OpenFolder();

            if (opendlg.ShowDialog() != true)
            {
                return;
            }

            if (string.IsNullOrEmpty(opendlg.SelectedPath))
            {
                return;
            }

            Directory = opendlg.SelectedPath;
#endif
        }

        private void DoClearCommand()
        {
            Directory = null;
        }

        //-------------------------------------------------------------
        //
        //          Properties
        //
        //-------------------------------------------------------------
        public ICommandEX SelectedCommand { get; }
        public ICommandEX ClearCommand    { get; }

        /// <summary>
        /// 获取或设置 <see cref="Directory"/> 属性。
        /// </summary>
        public string Directory
        {
            get => _directory;
            set => TryFinishAndSetValue(ref _directory, value);
        }
    }
}